<template>
  <div :class="{'hidden':hidden, 'hz-pagination-container':Heze}" class="pagination-container">
    <el-pagination :background="background" :current-page.sync="currentPage" :page-size.sync="pageSize" :layout="layout" :page-sizes="sizeList || pageSizes" :pager-count="pagerCount" :total="total" v-bind="$attrs" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
</template>

<script>
import { scrollTo } from 'hc-core/utils/scroll-to'
import { mapState, mapActions } from "vuex";
export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default()
      {
        return [10, 20, 30, 50]
      }
    },
    // 移动端页码按钮的数量端默认值5
    pagerCount: {
      type: Number,
      default: document.body.clientWidth < 992 ? 5 : 7
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    },
    moduleName: {
      type: String,
      default: ''
    },
    viewType: {
      type: String,
      default: ''
    }
  },
  data()
  {
    return {
      // 菏泽主题
      Heze: window.HC_config.VUE_APP_HEZE
    };
  },
  created()
  {
    //获取办理方式列表
    this.getCs({
      dictKey: "DICT_PAGE_SIZE_LIST",
      code: "xtsz_page_size"
    })
  },
  computed: {
    ...mapState("dict", ["DICT_PAGE_SIZE_LIST"]),
    sizeList()
    {
      if (this.DICT_PAGE_SIZE_LIST)
      {
        let numSizeList = [];
        this.DICT_PAGE_SIZE_LIST.forEach(pre_item =>
        {
          numSizeList.push(Number(pre_item))
        });
        return numSizeList;
      }
      else
      {
        return null;
      }
    },
    currentPage: {
      get()
      {
        return this.page
      },
      set(val)
      {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get()
      {
        return Number(this.limit)
      },
      set(val)
      {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    
    ...mapActions("dict", ["getCs"]),
    handleSizeChange(val)
    {
      if (this.currentPage * val > this.total)
      {
        this.currentPage = 1
      }
      if (this.autoScroll)
      {
        scrollTo(0, 800)
      }
      console.log("---------修改每页数量-----------")
      localStorage.setItem("pageSize", Number(val));
      this.$emit('pagination', { page: this.currentPage, limit: val })
    },
    handleCurrentChange(val)
    {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll)
      {
        scrollTo(0, 800)
      }
      localStorage.setItem("currentPage" + '_' + this.moduleName + '_' + this.viewType, Number(val));
    }
  }
}
</script>

<style lang="scss" scoped>
.pagination-container {
  background: #fff;
  height: 35px;
  padding: 32px 16px;
  margin-top: 5px;
  margin-bottom: 10px;
  ::v-deep .el-pagination
  {
    top: 0;
  }
}
.hz-pagination-container {
  background: #ffffff6b !important;
}
.pagination-container.hidden {
  display: none;
}
</style>
